﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.DBM; 

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class ES_MoveRequest : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private MoveRequestMainBLL mreqBLL = new MoveRequestMainBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }


        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }
        //Initializing the new variable updating ViewState data.
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                lblEmpSeq.Text = ssMng.EmpSeq;
                this.ViewState.Add("IsUserControlPostBack", true);    
            }

            if (DataItem != null)
            {
                
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    cmbUpper_ItemsRequested(e, null);
                    lblReqSeq.Text = DataBinder.Eval(DataItem, "req_seq").ToString();
                    lblCreateWhen.Text = string.Format("{0:dd/MM/yyyy}", DataBinder.Eval(DataItem, "created_when"));
                    txtSeq.Text = DataBinder.Eval(DataItem, "ref_no").ToString();
                    txtRemark.Text = DataBinder.Eval(DataItem, "remarks").ToString();
                    cmbUpper.SelectedValue = DataBinder.Eval(DataItem, "approved_seq").ToString() + ":" + DataBinder.Eval(DataItem, "approved_position").ToString();
                }
                else
                {
                    lblCreateWhen.Text = string.Format("{0:dd/MM/yyyy}", DateTime.Today);
                    txtSeq.Text = mreqBLL.getSeq(int.Parse(lblEmpSeq.Text)).ToString();
                }
            }
        }

        protected void cmbUpper_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            UpperComboBLL obj = new UpperComboBLL();
            DataTable dataTable = new DataTable();
            dataTable = obj.ListUpperPosition(Int32.Parse(lblEmpSeq.Text.Trim()), 1, 0);
            if (dataTable != null)
            {
                foreach (DataRow dataRow in dataTable.Rows)
                {
                    RadComboBoxItem item = new RadComboBoxItem();

                    item.Text = (string)dataRow["full_name"];
                    item.Value = dataRow["emp_seq"].ToString();

                    item.Attributes.Add("full_name", (string)dataRow["full_name"]);
                    item.Attributes.Add("exec_pos_name", (string)dataRow["exec_pos_name"]);
                    item.Attributes.Add("class_name", (string)dataRow["class_name"]);

                    item.Value += ":" + (string)dataRow["exec_pos_name"];

                    cmbUpper.Items.Add(item);
                    item.DataBind();
                }
            }
        }


        protected void btnAddnew_Click(object sender, EventArgs e)
        {                                       
            mreqBLL.Insert_hrm_es_move_requests(Convert.ToInt32(lblEmpSeq.Text)
                , txtSeq.Text.ToString()
                , int.Parse(cmbUpper.SelectedValue.Split(new char[] { ':' })[0])
                , cmbUpper.SelectedValue.Split(new char[] { ':' })[1]
                , txtRemark.Text
                , ssMng.EmpSeqI);                 
           
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {         
            mreqBLL.Update_hrm_es_move_requests(int.Parse(lblReqSeq.Text)
                , txtSeq.Text.ToString()
                , int.Parse(cmbUpper.SelectedValue.Split(new char[] { ':' })[0])
                , cmbUpper.SelectedValue.Split(new char[] { ':' })[1]
                , txtRemark.Text
                , ssMng.EmpSeqI);    
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }
    }
}